package com.sys.sprinngboot.goodtheater.controller;

import com.sys.sprinngboot.goodtheater.bean.*;
import com.sys.sprinngboot.goodtheater.dao.OrderMapper;
import com.sys.sprinngboot.goodtheater.dao.SceneMapper;
import com.sys.sprinngboot.goodtheater.service.BookTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class BookTicketController {
    @Autowired
    BookTicketService bookTicketService;
    //得到当前订单

    @RequestMapping(value = "/ticket")
    public String ticket(HttpSession session,Model model){
        User user = (User)session.getAttribute("login");
        List<OrderVo> orderVoList = bookTicketService.ticket(user);
        //工具类倒序
        Collections.reverse(orderVoList);
        System.out.println(orderVoList);
        model.addAttribute("orders",orderVoList);
        return "ticket";
    }
    //注销
    @RequestMapping(value = "/doLogout")
    public String logOut(HttpSession httpSession){
        httpSession.setAttribute("login",null);
        return "movie-list";
    }

    @RequestMapping(value = "/book-final/{num}")
    public String book4(@PathVariable("num") String ticketNum,
                        HttpSession session,Model model){
        User user = (User)session.getAttribute("login");
        List<OrderVo> orderVoList = bookTicketService.ticket(user);
        System.out.println(orderVoList);
        OrderVo orderVo = orderVoList.get(orderVoList.size() - 1);
        if(orderVo == null){
            return "404";
        }
        model.addAttribute("order",orderVo);
        return "book-final";
    }
    //支付订单，支付成功则锁座
    @PostMapping(value = "/order")
    public ResponseEntity addOrder(@RequestBody BookForm bookForm,HttpSession session){
        Order order = new Order();
        StringBuilder ticketNum = new StringBuilder();
        ticketNum.append(bookForm.getSiteNum());
        User user = (User) session.getAttribute("login");
        order.setUserId(user.getId());
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setSceneId(bookForm.getSceneId());
        Scene scene = bookTicketService.scenceById(bookForm);
        double price = scene.getPrice();
        order.setTicketNum(ticketNum.toString());
        String[] site = bookForm.getBookedSits().split(",");
        order.setTotalPrice(price * site.length);
        StringBuilder sb = new StringBuilder();
        for (String s : site) {
            sb.append(s + '#');
        }
        Scene sceneById = bookTicketService.scenceById(bookForm);
        String bookedSeat = sceneById.getBookedSeat();
        StringBuilder sb2 = new StringBuilder();
        sb2.append(bookedSeat);
        sb2.append('#');
        sb2.append(sb.toString());
        sb2.deleteCharAt(sb2.length()-1);
        sceneById.setBookedSeat(sb2.toString());
        order.setSeat(sb.toString().substring(0, sb.length() - 1));
        int result = bookTicketService.addOrder(order);
        Map<String, String> map = new HashMap<>();
        if (result != 0) {
            map.put("message", "支付成功！");
            bookTicketService.updateScene(sceneById);
            map.put("ticketNum", ticketNum.toString());
            return ResponseEntity.ok(map);
        }
        map.put("message", "支付失败！");
        return ResponseEntity.ok(map);
    }
    //选择观看场次
    @RequestMapping(value = "/book3-buy")
    public String book3(BookForm bookForm,Model model){
        Scene scene = bookTicketService.scenceById(bookForm);
        BookForm booking = new BookForm();
        String[] site = bookForm.getBookedSits().split(",");
        double price =site.length*scene.getPrice();
        if(scene == null|| site.length != bookForm.getSiteNum()){
            return "404";
        }
        booking.setMovieName(scene.getMovieName());
        booking.setSceneId(scene.getId());
        booking.setPrice(scene.getPrice());
        booking.setTotalPrice(price);
        booking.setBookedSits(bookForm.getBookedSits());
        booking.setSiteNum(site.length);
        booking.setMovieName(scene.getMovieName());
        model.addAttribute("bookForm", booking);
        return "book3-buy";
    }
    //选择座位
    @RequestMapping(value = "/book2")
    public String book2(@RequestParam("sceneId") Integer sceneId, Model model) {
        BookForm bookForm = new BookForm();
        Scene scene = bookTicketService.scenceByIntId(sceneId);
        System.out.println(scene);
        if (scene == null) {
            return "404";
        }
        bookForm.setMovieName(scene.getMovieName());
        bookForm.setShowtime(scene.getShowTime());
        bookForm.setSceneId(sceneId);
        bookForm.setPrice(scene.getPrice());
        model.addAttribute("bookForm", bookForm);
        model.addAttribute("sits", scene.getBookedSeat());
        return "book2";
    }

    //选择剧目
    @RequestMapping(value = "/book1")
    public String book1(@RequestParam("dramaId") Integer movieId
            , Model model){
        List<Scene> scenes = bookTicketService.book1(movieId);
        model.addAttribute("scenes",scenes);
        return "book1";
    }


}
